package com.ssyt.yt1.day26;

import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ssyt.yt1.common.util.DBUtil;
import com.ssyt.yt1.day25.User;

/**
 * CRUD
 *
 * @author Hsu.
 * @since 2015年12月28日
 */
public class CRUDDemo {

	/**
	 * 增
	 * 
	 * @throws SQLException
	 * @throws NullPointerException
	 */
	public static void test1(User user) {
		if (user == null) {
			throw new NullPointerException("传入的对象不能为空！");
		}
		List<Object> paramList = new ArrayList<>();
		paramList.add(user.getIdcard());
		paramList.add(user.getUsername());
		paramList.add(user.getPassword());
		paramList.add(user.getSalary());
		paramList.add(user.getBirthday());
		paramList.add(user.getMemo());
		int row = DBUtil.executeUpdate(
				"insert into user values(null, ?, ?, ?, ?, ?, ?)", paramList);
		System.out.println("受影响的行数：" + row);
	}

	/**
	 * 根据id删除
	 */
	public static void test2() {
		int row = DBUtil.executeUpdate("delete from user where id=?", 1);
		System.out.println("受影响的行数：" + row);
	}

	/**
	 * 查询
	 * 
	 * @throws SQLException
	 */
	public static void test3() throws SQLException {
		List<Map<String, Object>> resultList = DBUtil
				.executeQuery("select * from user");
		for (Map<String, Object> map : resultList) {
			System.out.println(map.get("id") + ", " + map.get("idcard") + ", "
					+ map.get("username") + ", " + map.get("password") + ", "
					+ map.get("salary") + ", " + map.get("birthday") + ", "
					+ map.get("memo"));
		}
	}

	public static void main(String[] args) throws ClassNotFoundException,
			SQLException, ParseException {
		// User user = new User();
		// user.setIdcard("1234");
		// user.setUsername("李四");
		// user.setPassword("admin");
		// user.setSalary(new BigDecimal("999.90"));
		// user.setBirthday(Util.parse("1995-12-11", "yyyy-MM-dd"));
		// user.setMemo(".........");
		// test1(user);

		// test2();

		test3();
	}
}
